package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.mapper.RealAuthMapper;
import cn.wolfcode.p2p.base.qo.QueryObject;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.BitStateUtil;
import cn.wolfcode.p2p.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service@Transactional
public class RealAuthServiceImpl implements IRealAuthService {

    @Autowired
    private RealAuthMapper realAuthMapper;
    @Autowired
    private IUserInfoService userInfoService;

    @Override
    public int save(RealAuth realAuth) {
        return realAuthMapper.insert(realAuth);
    }

    @Override
    public int update(RealAuth realAuth) {
        return realAuthMapper.updateByPrimaryKey(realAuth);
    }

    @Override
    public RealAuth get(Long id) {
        return realAuthMapper.selectByPrimaryKey(id);
    }

    @Override
    public void realAuthApply(RealAuth realAuth) {
        //更新信息
        RealAuth ra = new RealAuth();
        ra.setAddress(realAuth.getAddress());
        ra.setApplier(UserContext.getCurrent());
        ra.setApplyTime(new Date());
        ra.setBornDate(realAuth.getBornDate());
        ra.setIdNumber(realAuth.getIdNumber());
        ra.setImage1(realAuth.getImage1());
        ra.setImage2(realAuth.getImage2());
        ra.setRealName(realAuth.getRealName());
        ra.setSex(realAuth.getSex());
        ra.setState(RealAuth.STATE_NORMAL); //待审核状态
        realAuthMapper.insert(ra);
        //把实名认证的id保存到userInfo中
        UserInfo current = userInfoService.getCurrent();
        current.setRealAuthId(ra.getId());
        userInfoService.update(current);
    }

    @Override
    public PageInfo queryPage(QueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        List result = realAuthMapper.queryData(qo);
        return new PageInfo(result);
    }

    @Override
    public void audit(Long id, Integer state, String remark) {
        //根据id获取当前用户处于待审核状态
        RealAuth realAuth = realAuthMapper.selectByPrimaryKey(id);
        if (realAuth != null && realAuth.getState() == RealAuth.STATE_NORMAL){
            //设置审核人,审核时间,审核状态.审核备注
            realAuth.setAuditor(UserContext.getCurrent());
            realAuth.setAuditTime(new Date());
            realAuth.setRemark(remark);
            realAuth.setState(state);
            //如果审核未通过,设置userInfo中的realAuthId为null
            UserInfo applierUserInfo = userInfoService.select(realAuth.getApplier().getId()); //获取申请人
            if (state == RealAuth.STATE_REJECT) {
                applierUserInfo.setRealAuthId(null);
            } else {
                //审核通过,设置实名认证状态码
                applierUserInfo.addState(BitStateUtil.HAS_REALAUTH);
                applierUserInfo.setIdNumber(realAuth.getIdNumber());
                applierUserInfo.setRealName(realAuth.getRealName());
            }
            userInfoService.update(applierUserInfo);
            this.realAuthMapper.updateByPrimaryKey(realAuth);
        }
    }
}
